package comLibs.findingPath
{
	/**
	 * Server as a visual representation of a grid of nodes used in a pathfinding solution. 
	 */	
	import comLibs.findingPath.Grid;
	import comLibs.findingPath.Node;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class GridView extends Sprite
	{
		private var _cellSize:int = 20;
		private var _grid:Grid;
		public function GridView(grid:Grid)
		{
			super();
			_grid = grid;
			drawGrid();
			findPath();
			addEventListener(MouseEvent.CLICK,onGridClick);
		}
		/**
		 * Draws the given grid, coloring each cell according to its state. 
		 * 
		 */		
		private function drawGrid():void
		{
			graphics.clear();
			for(var i:int = 0;i<_grid.numCols;i++)
			{
				for(var j:int=0;j<_grid.numRows;j++)
				{
					var node:Node = _grid.getNode(i,j);
					graphics.lineStyle(0);
					graphics.beginFill(getColor(node));
					graphics.drawRect(i*_cellSize,j*_cellSize,_cellSize,_cellSize);
				}
			}
		}// end func
		private function getColor(node:Node):uint
		{
			if(!node.walkable) return 0;
			if(node == _grid.startNode) return 0x666666;
			if(node == _grid.endNode) return 0x888888;
			return 0xffffff;
		}
		/**
		 * Handles the click event on GridView. Finds the clicked on cell and  
		 * @param e
		 * 
		 */		
		private function onGridClick(e:MouseEvent):void
		{
			var xpos:int = Math.floor(e.localX/_cellSize);
			var ypos:int = Math.floor(e.localY/_cellSize);
			_grid.setWalkable(xpos,ypos,!_grid.getNode(xpos,ypos).walkable);
			drawGrid();
			findPath();
		}
        /**
         * Creates an instance of Ami and uses it to find a path. 
         * 
         */        
		private function findPath():void
		{
			var ami:Ami = new Ami();
            if(ami.findPath(_grid))
            {
                showVisited(ami);
                showPath(ami);
            }
		}// end func
        /**
         * Highlights all nodes that have been visited. 
         * @param ami
         * 
         */        
        private function showVisited(ami:Ami):void
        {
            var visited:Array = ami.visited;
            for(var i:int=0;i<visited.length;i++)
            {
                graphics.beginFill(0xcccccc);
                graphics.drawRect(visited[i].x*_cellSize,
                                  visited[i].y*_cellSize,
                                  _cellSize,
                                  _cellSize
                                  );
                graphics.endFill();
            }
        }//end func
        private function showPath(ami:Ami):void
        {
            var path:Array = ami.path;
            for(var i:int = 0;i<path.length;i++)
            {
                graphics.lineStyle(0);
                graphics.beginFill(0);
                graphics.drawCircle(path[i].x*_cellSize+_cellSize/2,
                                    path[i].y*_cellSize+_cellSize/2,
                                    _cellSize/3
                                    );
            }
             
        }
        
	}
}